package model;

//import java.util.ArrayList;
import java.util.HashMap;
import org.jbox2d.common.Vec2;
import org.newdawn.slick.Image;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;

public class Assets {
	public static Assets assets;
	public HashMap<String, Image> imageMap;
	public HashMap<String, Sound> soundMap;
	public HashMap<String, Music> musicMap;
	public HashMap<String, Entity> entityMap;

	private Assets() {
		imageMap = new HashMap<String, Image>();
		soundMap = new HashMap<String, Sound>();
		entityMap = new HashMap<String, Entity>();
		musicMap = new HashMap<String, Music>();
	}

	public static Assets getInstance() {
		if (assets == null)
			assets = new Assets();
		return assets;
	}

	public void loadEntity(String filename, Entity entity) {
		entityMap.put(filename, entity);
	}

	public Entity getEntity(String filename) {
		return (Entity) entityMap.get(filename).clone();
	}

	public void loadImage(String filename) {
		Image image;
		try {
			image = new Image(filename);
			image.clampTexture();
			imageMap.put(filename, image);
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

	public Image getImage(String filename) {
		return imageMap.get(filename);
	}

	public void loadSound(String filename) {
		Sound sound;
		try {
			sound = new Sound(filename);
			soundMap.put(filename, sound);
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

	public Sound getSound(String filename) {
		return soundMap.get(filename);
	}

	public void loadMusic(String filename) {
		Music music;
		try {
			music = new Music(filename);
			musicMap.put(filename, music);
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

	public Music getMusic(String filename) {
		return musicMap.get(filename);
	}

	public void loadAssets() {
		Entity entity;
		// ArrayList<Entity> e = new ArrayList<Entity>();
		// ArrayList<Joints> j = new ArrayList<Joints>();
		// Joints j1, j2;
		Vec2[] vertices;

		// load images
		loadImage("assets/gfx/block.png");
		loadImage("assets/gfx/ball.png");
		loadImage("assets/gfx/horizon.png");
		loadImage("assets/gfx/parallax.png");
		loadImage("assets/gfx/smile.png");
		loadImage("assets/gfx/dirt.png");
		loadImage("assets/gfx/ice_slant_left.png");
		loadImage("assets/gfx/ice_slant_right.png");
		loadImage("assets/gfx/car_body.png");
		loadImage("assets/gfx/wheel.png");
		loadImage("assets/gfx/girl.png");
		loadImage("assets/gfx/bullet.png");
		loadImage("assets/gfx/waterline.png");
		loadImage("assets/gfx/crate.png");
		loadImage("assets/gfx/house.png");
		loadImage("assets/gfx/rock.png");
		loadImage("assets/gfx/black.png");
		loadImage("assets/gfx/grey50.png");
		loadImage("assets/gfx/background.png");
		loadImage("assets/gfx/underwater.png");
		loadImage("assets/gfx/underwater1.png");
		loadImage("assets/gfx/underwater2.png");
		loadImage("assets/gfx/black50.png");
		loadImage("assets/gfx/cyan50.png");
		loadImage("assets/gfx/water50.png");
		loadImage("assets/gfx/parallax_night_better.png");
		loadImage("assets/gfx/fish.png");
		loadImage("assets/gfx/man.png");
		loadImage("assets/gfx/woman.png");
		loadImage("assets/gfx/rocket.png");
		loadImage("assets/gfx/health.png");

		// load sound effects
		loadSound("assets/sfx/zoom.wav");
		loadSound("assets/sfx/splash.wav");
		loadSound("assets/sfx/water.wav");
		loadSound("assets/sfx/ricochet.wav");
		loadSound("assets/sfx/dink.wav");
		loadSound("assets/sfx/pop.wav");
		loadSound("assets/sfx/underwater.wav");
		loadSound("assets/sfx/enchant.ogg");

		// load music
		// loadMusic("assets/music/music.ogg");

		// load girl entity
		vertices = new Vec2[5];
		vertices[0] = new Vec2();
		vertices[1] = new Vec2();
		vertices[2] = new Vec2();
		vertices[3] = new Vec2();
		vertices[4] = new Vec2();
		vertices[0].x = 0;
		vertices[0].y = 169;
		vertices[1].x = -99;
		vertices[1].y = 72;
		vertices[2].x = -54;
		vertices[2].y = -173;
		vertices[3].x = 30;
		vertices[3].y = -173;
		vertices[4].x = 99;
		vertices[4].y = 66;

		entity = new Entity();
		entity.name = "girl";
		entity.type = Entity.POLYGON;
		entity.image = getImage("assets/gfx/girl.png");
		entity.scale = 2;
		entity.sizeX = 1;
		entity.sizeY = 1;
		entity.vertices = vertices;
		entity.friction = 0f;
		entity.restitution = 0f;
		entity.density = 10;
		entity.orient = true;
		entity.linearDamping = .5f;
		entity.isOrientable = true;
		entity.register("behaviors.Orient");
		entity.register("behaviors.Splash");
		entity.register("behaviors.Control");
		entity.register("behaviors.Gun");
		loadEntity("assets/entities/girl.txt", entity);

		// load ball entity
		entity = new Entity();
		entity.name = "ball";
		entity.type = Entity.CIRCLE;
		entity.image = this.getImage("assets/gfx/ball.png");
		entity.radius = 0.5f;
		entity.density = 2;
		entity.register("behaviors.Balloon");
		entity.register("behaviors.Splash");
		loadEntity("assets/entities/ball.txt", entity);

		// load fish entity
		entity = new Entity();
		entity.name = "fish";
		entity.type = Entity.CIRCLE;
		entity.image = this.getImage("assets/gfx/fish.png");
		entity.radius = 0.25f;
		entity.density = 10;
		entity.isSensor = true;
		entity.register("behaviors.Fish");
		entity.register("behaviors.Splash");
		entity.fixedRotation = true;
		loadEntity("assets/entities/fish.txt", entity);

		// load house entity (backdrop test)
		entity = new Entity();
		entity.name = "health";
		entity.type = Entity.POLYGON;
		entity.image = this.getImage("assets/gfx/health.png");
		entity.sizeX = .35f;
		entity.sizeY = .35f;
		entity.isSensor = true;
		entity.density = 0;
		entity.register("behaviors.Health");
		loadEntity("assets/entities/health.txt", entity);
		
		// load house entity (backdrop test)
		entity = new Entity();
		entity.name = "house";
		entity.type = Entity.POLYGON;
		entity.image = this.getImage("assets/gfx/house.png");
		entity.sizeX = 5;
		entity.sizeY = 5;
		entity.isSensor = true;
		entity.density = 0;
		loadEntity("assets/entities/house.txt", entity);

		// load house entity (backdrop test)
		entity = new Entity();
		entity.name = "rocket";
		entity.type = Entity.POLYGON;
		entity.image = this.getImage("assets/gfx/rocket.png");
		entity.sizeX = 2.5f;
		entity.sizeY = 2.5f;
		entity.isSensor = true;
		entity.density = 0;
		loadEntity("assets/entities/rocket.txt", entity);

		// load bullet entity
		entity = new Entity();
		entity.name = "bullet";
		entity.type = Entity.CIRCLE;
		entity.image = getImage("assets/gfx/bullet.png");
		entity.radius = .15f;
		entity.restitution = 0.75f;
		entity.density = 3f;
		entity.isBullet = true;
		entity.facing = true;
		entity.register("behaviors.Bullet");
		loadEntity("assets/entities/bullet.txt", entity);

		// load block entity
		entity = new Entity();
		entity.name = "block";
		entity.type = Entity.POLYGON;
		entity.image = getImage("assets/gfx/block.png");
		entity.sizeX = .5f;
		entity.sizeY = .5f;
		entity.density = 0f;
		loadEntity("assets/entities/block.txt", entity);

		// load block entity
		entity = new Entity();
		entity.name = "block";
		entity.type = Entity.POLYGON;
		entity.image = getImage("assets/gfx/rock.png");
		entity.sizeX = .5f;
		entity.sizeY = .5f;
		entity.density = 0f;
		loadEntity("assets/entities/rock.txt", entity);

		// load crate entity
		entity = new Entity();
		entity.name = "crate";
		entity.type = Entity.POLYGON;
		entity.image = getImage("assets/gfx/crate.png");
		entity.sizeX = 1;
		entity.sizeY = 1;
		entity.density = 5f;
		entity.register("behaviors.Splash");
		loadEntity("assets/entities/crate.txt", entity);

		// load ice slant left entity
		vertices = new Vec2[4];
		vertices[0] = new Vec2();
		vertices[1] = new Vec2();
		vertices[2] = new Vec2();
		vertices[3] = new Vec2();
		vertices[0].x = -93;
		vertices[0].y = -17;
		vertices[1].x = -65;
		vertices[1].y = -61;
		vertices[2].x = 94;
		vertices[2].y = 34;
		vertices[3].x = 67;
		vertices[3].y = 74;

		entity = new Entity();
		entity.name = "ice slant left";
		entity.type = Entity.POLYGON;
		entity.image = getImage("assets/gfx/ice_slant_left.png");
		entity.vertices = vertices;
		entity.scale = 3;
		entity.sizeX = 1.5f;
		entity.sizeY = 1.5f;
		entity.friction = 0f;
		entity.density = 0f;
		loadEntity("assets/entities/ice_slant_left.txt", entity);

		// load ice slant right entity
		vertices = new Vec2[4];
		vertices[0] = new Vec2();
		vertices[1] = new Vec2();
		vertices[2] = new Vec2();
		vertices[3] = new Vec2();
		vertices[0].x = -91;
		vertices[0].y = 29;
		vertices[1].x = 70;
		vertices[1].y = -64;
		vertices[2].x = 93;
		vertices[2].y = -22;
		vertices[3].x = -70;
		vertices[3].y = 70;

		entity = new Entity();
		entity.name = "ice slant right";
		entity.type = Entity.POLYGON;
		entity.image = getImage("assets/gfx/ice_slant_right.png");
		entity.vertices = vertices;
		entity.scale = 3;
		entity.sizeX = 1.5f;
		entity.sizeY = 1.5f;
		entity.friction = 0f;
		entity.density = 0f;
		loadEntity("assets/entities/ice_slant_right.txt", entity);

		// load wheel entity
		// entity = new Entity();
		// entity.name = "wheel";
		// entity.type = Entity.CIRCLE;
		// entity.image = getImage("assets/gfx/wheel.png");
		// entity.radius = .5f;
		// entity.restitution = 0.1f;
		// entity.friction = 10;
		// entity.density = 10;
		// loadEntity("assets/entities/wheel.txt", entity);
		//
		// // load car body entity
		// vertices = new Vec2[6];
		// vertices[0] = new Vec2();
		// vertices[1] = new Vec2();
		// vertices[2] = new Vec2();
		// vertices[3] = new Vec2();
		// vertices[4] = new Vec2();
		// vertices[5] = new Vec2();
		// vertices[0].x = -164;
		// vertices[0].y = 5;
		// vertices[1].x = -160;
		// vertices[1].y = -34;
		// vertices[2].x = 163;
		// vertices[2].y = -34;
		// vertices[3].x = 161;
		// vertices[3].y = 10;
		// vertices[4].x = 41;
		// vertices[4].y = 37;
		// vertices[5].x = -57;
		// vertices[5].y = 37;
		//
		// entity = new Entity();
		// entity.name = "car_body";
		// entity.type = Entity.POLYGON;
		// entity.image = getImage("assets/gfx/car_body.png");
		// entity.scale = 6.75f;
		// entity.sizeX = 6.75f / 2f;
		// entity.sizeY = 6.75f / 2f;
		// entity.vertices = vertices;
		// entity.friction = 0f;
		// entity.restitution = 0f;
		// entity.density = 25;
		// loadEntity("assets/entities/car_body.txt", entity);
		// // create car parts
		// e = new ArrayList<Entity>();
		// e.add(getEntity("assets/entities/wheel.txt"));
		// e.get(0).modX = -1.7f;
		// e.get(0).modY = 0f;
		// e.get(0).name = "back_wheel";
		// e.add(getEntity("assets/entities/wheel.txt"));
		// e.get(1).modX = 2.29f;
		// e.get(1).modY = 0f;
		// e.get(1).name = "front_wheel";
		// e.add(getEntity("assets/entities/car_body.txt"));
		// e.get(2).modX = 0f;
		// e.get(2).modY = .8f;
		// e.get(2).name = "car_body";
		//
		// // add wheels to car
		// j = new ArrayList<Joints>();
		// j1 = new Joints();
		// j1.entity1 = e.get(0);
		// j1.entity2 = e.get(2);
		// j2 = new Joints();
		// j2.entity1 = e.get(1);
		// j2.entity2 = e.get(2);
		// j.add(j1);
		// j.add(j2);
		//
		// // create completed car
		// entity = new Entity();
		// entity.name = "car";
		// entity.type = Entity.COMPOUND;
		// entity.entityList = e;
		// entity.jointList = j;
		// loadEntity("assets/entities/car.txt", entity);
	}
}
